<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <title>试题管理</title>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />

    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/amazeui.min.css"/>

    <script src="../scripts/jquery.min.js"></script>
    <script src="../layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
</head>
<body>

<input type="hidden" id="flag_manager" value=<%=session.getAttribute("flag_manager")%> />


<div class="x-body">
    <div class="layui-row">
        <div class="layui-form layui-col-md12 x-so" id="searchByInfo">
            <input class="layui-input" placeholder="试题科目" name="question_class" id="question_class_input" onblur="search_blur()">
            <input class="layui-input" placeholder="章节(数字，1,2,3等)" name="question_chapter" id="question_chapter_input" onblur="search_blur()">
            <input class="layui-input" placeholder="试题来源" name="question_ori" id="question_ori" autocomplete="off" onblur="search_blur()">
            <button class="layui-btn" onclick="search_button()"><i class="layui-icon">&#xe615;</i>
            </button>
        </div>
    </div>
    <xblock>
        <button class="layui-btn layui-btn-danger" onclick="question_table_delete_many()"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" onclick="question_table_add()"><i class="layui-icon"></i>添加</button>
        <button class="layui-btn layui-btn-primary" onclick="question_table_files_upload()"><i class="layui-icon">&#xe62f;</i> 导入</button>
        <a href="">
        <span class="x-right" style="line-height:40px">
        <button class="layui-btn"><i class="layui-icon">&#xe65c;</i>刷新</button>
        </span>
        </a>
    </xblock>
    <table class="layui-table" id="question_table_id">
    </table>
</div>


<script>
    var tableIns;
    var table;
    var layer;

    var total_pages;
    var current_page;
    var limit;
    var current_rows;

    layui.use('layer', function(){
        layer = layui.layer;
    });
    layui.use('table', function(){
        table = layui.table;

        tableIns=table.render({
            elem: '#question_table_id'
            ,height: 312
            ,url:"${pageContext.request.contextPath}/question/question_table_select.do" //数据接口
            ,where:{
                "question_class":$("#question_class_input").val(),
                "question_chapter":$("#question_chapter_input").val(),
                "question_ori":$("#question_ori").val()
            }
            ,parseData:function (res){
                return {
                    "code":res.code,
                    "msg":res.msg,
                    "count":res.count,
                    "data":res.data
                }
            }
            ,page: true //开启分页
            ,limit: 10
            ,limits: [10,15,20,25,30]
            ,cols: [[ //表头
                {field: 'checkbox',title: '选框', width:"10%", type:"checkbox", align:"center", fixed: 'left'}
                ,{field: 'question_id', title: 'ID', width:"12%", sort: true}
                ,{field: 'question_content', title: '内容hide', hide:true,}
                ,{field: 'question_content_link', title: '内容', width:"10%", toolbar: "#question_content_link"}
                ,{field: 'question_option', title: '选项hide', hide:true,}
                ,{field: 'question_option_link', title: '选项', width:"10%", toolbar: "#question_option_link"}
                ,{field: 'question_answer', title: '答案', width:"9%"}
                ,{field: 'question_img', title: '图片', width: "8%"}
                ,{field: 'question_class', title: '类型', width: "10%"}
                ,{field:'question_chapter',title:'章节',width:"8%"}
                ,{field: 'question_ori', title: '来源', width: "8%"}
                ,{field: 'operate', title: '操作', width: "15%", align:"center", toolbar:"#endBar", fixed: 'right' }
            ]],
            done:function(res,curr,count){
                current_page=curr;
                total_pages=Math.ceil(count/this.limit);
                limit=this.limit;
                current_rows=count-(total_pages-1)*limit;

            }
        });

    });

    function changePage(curr) {
        tableIns.reload({
            where: { //设定异步数据接口的额外参数，任意设
                "question_class":$("#question_class_input").val(),
                "question_chapter":$("#question_chapter_input").val(),
                "question_ori":$("#question_ori").val()
            }
            , page: {
                curr: curr //重新从第 curr 页开始
            }
        });
    }
    function refreshPage(){
       changePage(current_page);
    }

    function search_blur(){
       changePage(1);
    }
    function search_button(){
        var question_class=$("#question_class_input").val();
        var question_chapter=$("#question_chapter_input").val();
        var question_ori=$("#question_ori").val();
        if(question_class==""&&question_chapter==""&&question_ori==""){
            layer.msg("没有填写查询信息，采用默认查询");
        }else{
            layer.msg("查询成功");
        }
    }

    function question_table_delete_many(){
        var check=table.checkStatus('question_table_id');
        var mydata=check.data;
        var question_id_array=[];
        if(mydata.length==0){
            layer.msg("您还没选中任何选项")
        }else{
            for(var i=0;i<mydata.length;i++){
                question_id_array.push(mydata[i].question_id);
            }
            // console.log(news_id_array);
            layer.confirm('确定删除？',{btn:['确定','取消']},
                function(){
                    if($("#flag_manager").val()!="null") {
                        $.ajax({
                            url:"${pageContext.request.contextPath}/question/question_table_delete_many.do",
                            dataType:"json",
                            data:{
                                "question_id_array":JSON.stringify(question_id_array)
                            },
                            success:function(data,status){
                                if(status=="success") {
                                    layer.msg("选中项已被删除", {icon: 1});
                                    refreshPage();
                                }
                            }
                        });
                    }else{
                        layer.msg("删除失败，您不是管理员");
                    }
                },function(){
                    layer.msg("取消删除");
                }
            );
        }


    }
    function question_table_add(){
        var w=$(window).width()*0.9;
        var h=$(window).height() - 50;
        // console.log(limit);
        // console.log(current_rows);
        layer.open({
            type: 2,
            area: [w+'px', h +'px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade:0.4,
            title: "添加试题信息",
            content: "question_table_add.jsp",
            cancel: function(index,layero){
                var flag_add=layer.getChildFrame('#flag_add',index).val();
                if(flag_add=="yes") {
                    var curr_add_rows=layer.getChildFrame('#curr_add_rows',index).val();
                    var curr_remove=Math.floor((parseInt(curr_add_rows)+current_rows)/limit);
                    changePage(total_pages+curr_remove);
                }
            }
        });
    }
    function question_table_edit(question_id){
        var w=$(window).width()*0.9;
        var h=$(window).height() - 50;
        layer.open({
            type: 2,
            area: [w+'px', h +'px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade:0.4,
            title: "修改试题信息",
            content: "question_table_edit.jsp?question_id="+question_id,
            cancel: function(index,layero){
                var flag_edit=layer.getChildFrame('#flag_edit',index).val();
                if(flag_edit=="yes") {
                    changePage(current_page);
                }
            }
        });
    }
    function question_table_delete_one(question_id){
        layer.confirm('确定删除？',{btn:['确定','取消']},
            function(){
                if($("#flag_manager").val()!="null"){
                    $.ajax({
                        url:"${pageContext.request.contextPath}/question/question_table_delete_one.do",
                        data:{
                            "question_id":question_id
                        },
                        success:function(data,status){
                            if(status=="success"){
                                layer.msg("删除成功",{icon:1});
                                refreshPage();
                            }
                        }
                    });
                }else{
                    layer.msg("删除失败，您不是管理员");
                }
            },function(){
                layer.msg("取消删除");
            }
        );

    }
    function question_table_files_upload(){
        // layer.alert("还没写");
        var w=$(window).width()*0.7;
        var h=$(window).height() - 50;
        layer.open({
            type: 2,
            area: [w+'px', h +'px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade:0.4,
            title: "导入试题信息",
            content: "question_file_add.jsp"
        });
    }
</script>
<script type="text/html" id="question_content_link">
    <a href="javascript:question_table_edit({{d.question_id}});" class="layui-table-link">{{d.question_content.substring(0,7)}}...</a>
</script>
<script type="text/html" id="question_option_link">
    <a href="javascript:question_table_edit({{d.question_id}});" class="layui-table-link">{{d.question_option.substring(0,7)}}...</a>
</script>
<script type="text/html" id="endBar">
    <a href="javascript:question_table_edit({{d.question_id}});" class="layui-table-link"><i class="layui-icon">&#xe642;</i></a>
    <a href="javascript:question_table_delete_one({{d.question_id}});" class="layui-table-link"><i class="layui-icon">&#xe640;</i></a>
</script>
</body>
</html>
